/**
  ETFDAQ Project -- online from ETFAna Online by SUN Yazhou
  \class TADaqData
  \brief providing a general interface to TAOnlineSource, shielding the underlying
  data generating mechanism, i.e., shm or socket
  This is supposed to be the base class of socket (client, server) and shm
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2023-06-24
  \date 2023-06-24 last modified
  \attention
  changelog
  <table>
  <tr>  <th>Date         <th>Author      <th>Description                    </tr>
  <tr>  <td>2023-06-24   <td>Asia Sun    <td>file created                   </tr>
  </table>

  \copyright Copyright (c) 2021-2024 Anyang Normal U. && IMP-CAS with LGPLv3 LICENSE
*/

#include <algorithm>
#include "TADaqData.h"
#include "TAQueue.h"
#include "TAMsg.h"

TADaqData::TADaqData(DaqType daqType) : fQueue(0), fIsRecvThAlive(false),
fIsSendThAlive(false), fDaqType(daqType){
  fsa.sa_flags = SA_SIGINFO; // specifying using sa_sigaction explicitly
  sigemptyset(&fsa.sa_mask);
  if(kPXI == fDaqType) strcpy(fDaqC, "PXI");
  else strcpy(fDaqC, "VME");
} // end ctor

TADaqData::~TADaqData(){}

// get a queue of size bytes in dst
// idx: index of the obtained event
void TADaqData::Get(void *dst, unsigned &idx){
  if(!fQueue) TAMsg::Error("TADaqData", "Get: fQueue not assigned.");
  fQueue->Pop(dst, idx);
} // end member function Get

// step forward (consume) n items
void TADaqData::Drop(int n){
  if(n <= 0) return;
  static unsigned idx;
  static int dst[online_shm_pxi::MAX_LEN];
  int nn = std::min(n, loaded());
  for(int i = 0; i < nn; i++) Get(dst, idx);
} // end member function Get

// reset the queue
void TADaqData::Reset(){
  if(fQueue) fQueue->Reset();
} // end member function Reset

int TADaqData::loaded() const{
  return fQueue->NLoaded();
} // end member function loaded